package api

import (
	"RedWork/dao"
	"RedWork/util"
	"RedWork/util/errmsg"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
)

func AddCar() gin.HandlerFunc {
	return func(c *gin.Context) {
		data := "success"
		status := true
		var uid interface{}
		unique := c.PostForm("unique_num")
		token_code, _ := c.Get("token_code")
		data = errmsg.GetErrMsg(token_code.(int))
		//uid, err := c.Cookie("uid")
		//if err != nil {
		//	fmt.Println("no cookie")
		//	status = false
		//	data = "NO COOKIE"
		//}
		//uid := c.PostForm("user_id")
		//gid := c.PostForm("good_id")
		//color := c.PostForm("color")
		//parm := c.PostForm("parm")
		if token_code != errmsg.SUCCSE {
			status = false
		} else {
			uid, _ = c.Get("uid")
			uniqueint, err := strconv.Atoi(unique)
			if err != nil {
				fmt.Println(err)
				status = false
				data = err.Error()
			}
			if status {
				err = dao.AddCar(dao.CarInfo{
					UserId:    uid.(int),
					UniqueNum: uniqueint,
				})
				if err != nil {
					status = false
					fmt.Println("err", err)
					data = err.Error()
				}
			}
			//gid, color, parm, _ := dao.QueryUniqueDetail(unique)
			//carInfo := dao.CarInfo{
			//	UserId: strconv.Itoa(uid.(int)),
			//	GoodId: gid,
			//	Color:  color,
			//	Parm:   parm,
			//}

		}

		c.JSON(200, gin.H{
			"status": status,
			"data":   data,
		})
	}
}

func QueryCar() gin.HandlerFunc {
	return func(c *gin.Context) {
		status := true
		uid := c.Query("userid")
		token_code, _ := c.Get("token_code")
		if token_code != errmsg.SUCCSE {
			status = false
		}
		res := dao.QueryCar(uid)
		fmt.Println(res)
		c.JSON(200, gin.H{
			"status": status,
			"data":   res,
		})
	}
}

func DeleteCar() gin.HandlerFunc {
	return func(c *gin.Context) {
		status, data, uidint := util.HandleTokenCode(c)
		if !status {
			fmt.Println(data)
			fmt.Println(uidint)
			return
		}
		car_id := c.Query("car_id")
		rows, err := dao.DeleteCar(car_id, uidint)
		if err != nil {
			status = false
			fmt.Println(err)
			data = errmsg.GetErrMsg(errmsg.PARM_NOT_RIGHT)
		}
		if rows == 0 {
			status = false
			data = "没有相关信息"
		}
		c.JSON(200, gin.H{
			"status": status,
			"data":   data,
		})
	}
}

func UpdateCar() gin.HandlerFunc {
	return func(c *gin.Context) {
		status, data, uidint := util.HandleTokenCode(c)
		if !status {
			fmt.Println(data)
			fmt.Println(uidint)
			return
		}
		car_id := c.Query("car_id")
		num := c.Query("num")
		rows, err := dao.UpdateCar(car_id, num, uidint)
		if err != nil {
			status = false
			fmt.Println(err)
			data = errmsg.GetErrMsg(errmsg.PARM_NOT_RIGHT)
		} else {
			if rows == 0 {
				status = false
				data = "没有相关信息"
			}
		}
		c.JSON(200, gin.H{
			"status": status,
			"data":   data,
		})
	}
}

func CleanCar() gin.HandlerFunc {
	return func(c *gin.Context) {
		status, data, uidint := util.HandleTokenCode(c)
		if !status {
			return
		}
		uid := c.Query("user_id")
		if strconv.Itoa(uidint) != uid {
			status = false
			data = errmsg.GetErrMsg(errmsg.PARM_NOT_RIGHT)
			c.JSON(200, gin.H{
				"status": status,
				"data":   data,
			})
			c.Abort()
			return
		}
		rows, err := dao.CleanCar(uid)
		if err != nil {
			fmt.Println(err)
			status = false
			data = err.Error()
			c.JSON(200, gin.H{
				"status": status,
				"data":   data,
			})
			c.Abort()
			return
		}
		c.JSON(200, gin.H{
			"status": status,
			"data":   fmt.Sprintf("删除条数为%d", rows),
		})
	}
}
